import Page from "$UI/wxsys/comps/reactContainer/page/Page";
import UserFlowBase from '$UI/pcx/userFlowBase';
import "$UI/mobile/processApproval.css";

export default class IndexPage extends Page {
    constructor(...args) {/*{{{*/this.comp = require("_comp").default; this._e = require("_event_").default;/*为代码提示动态插入的辅助代码,不要删除}}}*/
        super(...args);
        this.flowBase = new UserFlowBase(this, false);

    }

    // 页面显示
    onPageReady = (event) => {
        if (!this.params.title) {
            this.params.title = '采购申请单'
        }

        this.flowBase.onPageLoaded(event)
    }
    // 页面加载完成
    onPageShow = (event) => {
        // let a = []
        let selectSupplierList = wx.getStorageSync("selectSupplier");
        console.log(selectSupplierList, "选择下载")
        if (selectSupplierList.list.length) {
            let mainData = this.comp("mainData")
            mainData.setValue("repertoryPurchaseName", selectSupplierList.list[0].name)
            mainData.setValue("repertoryPurchaseId", selectSupplierList.list[0].fid)
        }
    }


    //申请表的刷新后事件
    onMainDataAfterRefresh = (event) => {
        if (!this.params.title) {
            this.params.title = '采购申请单'
        }
        this.flowBase.mainDataAfterRefresh(event)
    }

    // 流转前
    onWfmuiAdvanceBefore = (event) => {
        wx.showLoading({
            title: this.params.title + '正在流转...'
        })
    }

    // 流转成功
    onWfmuiAdvanceSuccess = (event) => {
        wx.hideLoading()
        this.hint(this.params.title + ' 流转成功', 'success', 1500)
    }

    //采购大类的值改变
    onPurchaseCategorySelectChange = ({ option }) => {
        console.log(option)
        let mainData = this.comp("mainData")
        mainData.setValue("purchaseSubclass", "")
        let filter = []
        let filterobj = {
            name: "purchaseCategory",
            value: option,
            op: "eq"
        }
        filter.push(filterobj)
        this.comp("purchaseSubData").setFilter("_system1_", filter)
        this.comp("purchaseSubData").refreshData()
    }
    //选择组织
    onOrgSelect0Change = ({ option }) => {
        let mainData = this.comp("mainData")
        this.comp("serviceRequestPlatformList").send({
            orgIds: mainData.getValue("purchaseRelevantDepFids")
        })

    }
    //选择平台
    onSelectPlatformChange = (res) => {
        console.log(res)
    }



    //增加
    onAddItemBtnClick = (event) => {
        this.onNewBtOnClickBase(event, 'detailData')
    }
    //表格中新增按钮的点击事件，显示弹框，编辑新增行的数据
    onNewBtOnClickBase(event, detailData, drawer) {
        const dtData = this.comp(detailData);
        let self = this
        dtData.newData({
            onSuccess: function (data) {
                console.log(data)
                setTimeout(function () {
                    dtData.to(data.data[0]);
                }, 200);
            }
        });
		// 客户都是客户反馈水电费
    }

    //输入单价计算当前行预算和总预算
    onUnitPriceInputChange = (event) => {
        console.log(event, "ee")
        debugger
        let mainData = this.comp("mainData")
        let detailData = this.comp("detailData")
        detailData.to(this.rows.fid)
        let curList = detailData.getRowByID(this.rows.fid)
        if (event.detail) {
            // console.log(1)
            detailData.setValue("totalSum", (event.detail * curList.quantity).toFixed(3))

            if (detailData.value.$mobx.values) {

                let mainSum = detailData.value.$mobx.values.reduce((acc, item) => {
                    return acc + item.totalSum
                }, 0)
                console.log(mainSum)
                mainData.setValue("unitPriceBudgetTotal", mainSum)
            }



        }
        console.log(detailData.value.$mobx.values)
    }

    onFlexboxCol4Click = (event) => {
        // console.log(event, "ee")
    }

    onList0RowClick = (event) => {
        let { detail: { row, index, id } } = event;
        // console.log(row, index, id)
        this.rows = row
    }
    //输入数量计算当前行预算和总预算
    onQuantityInputChange = (event) => {
        console.log(event, "ee")
        debugger
        let mainData = this.comp("mainData")
        let detailData = this.comp("detailData")
        detailData.to(this.rows.fid)
        let curList = detailData.getRowByID(this.rows.fid)
        if (event.detail) {
            // console.log(1)
            detailData.setValue("totalSum", (event.detail * curList.unitPrice).toFixed(3))

            if (detailData.value.$mobx.values) {

                let mainSum = detailData.value.$mobx.values.reduce((acc, item) => {
                    return acc + item.totalSum
                }, 0)
                console.log(mainSum)
                mainData.setValue("unitPriceBudgetTotal", mainSum)
            }
        }
        console.log(detailData.value.$mobx.values)
    }

    //删除
    onIcon1Click = (event) => {
        this.onDelTableCore(event, "detailData")
    }


    _delCurrentRow(data) {
        // debugger
        var that = this
        setTimeout(function () {
            let row = that.comp(data).getCurrentRow();
            let detailData = that.comp(data);
            detailData.to(row);
            detailData.deleteData();
        }, 200)
    }

    onDelTableCore(event, data) {
        event.stopPropagation()
        var that = this;
        wx.showModal({
            title: '提示',
            content: '确定删除当前行？',
            success: function (res) {
                if (res.confirm) {
                    that._delCurrentRow(data)
                } else {

                }
            }
        })
    }
    //删除后事件
    onDetailDataAfterDelete = (event/*{{{*/ = this._e.restData_afterDelete/*}}}*/) => {

        let mainData = this.comp("mainData")
        console.log(event, "删除后的数据")
        if (event.source.value.$mobx.values) {

            let mainSum = event.source.value.$mobx.values.reduce((acc, item) => {
                return acc + item.totalSum
            }, 0)
            console.log(mainSum)
            mainData.setValue("unitPriceBudgetTotal", mainSum)
        }
    }
}
